<template>
  <view class="container_page">
    <action-bar :options="navOptions"></action-bar>

    <view class="main">
      <step-one v-show="step.curStep === 0" ref="stepOne" />
    </view>

    <view class="footer">
      <view class="footer-submit" @click="navTo" v-if="!status_submit">注册</view>
      <view v-else-if="status_submit">正在保存中，请稍后</view>
    </view>
    <toast></toast>
  </view>
</template>

<script>
import stepOne from "./stepOne.vue";

let _ = require("lodash");
export default {
  components: { stepOne },
  data() {
    return {
      // 导航栏参数
      navOptions: {
        left: {
          icon: "back",
        },
        center: {
          title: "社会信息单位注册",
        },
      },
      status_submit: false, // 防抖
      step: {
        title: ["单位基本信息", "关键建筑信息", "租凭、场地信息"],
        curStep: 0,
      },
    };
  },
  methods: {
    // 注册
    navTo() {
      /*  result:校验结果,errMsg:校验失败的提示语
               校验引入的表单组件,如果该表单中有校验方法,
               校验错误 把结果改为false
               校验成功 取值
           */
      if (this.status_submit) {
        this.$showToast({
          show: true,
          text: "正在注册中，请稍后",
        });
      } else {
        this.status_submit = true;
        let errMsg = "";
        // 校验是否成功
        let result = true;
        let form = this.$refs.stepOne.form
        for (const key in form) {
          if (key == 'establishTime' || key == 'facilities' || key == 'facilitie' || key == 'number' || key == 'companyProfile' || key == 'imageValue' || key == 'businessLicense') {
            continue
          } else {
            if (form[key] == '' || form[key] == null) {
              result = false
            }
          }
        }
        // 如果校验通过
        if (result) {
          // 调用接口
          this.$store.dispatch("addUnitInfo", form).then((res) => {
            if (res.code == 200) {
              //! 二期项目需求变更: 注册通过后，后端直接返回一个token，随后跳转至首页即可
              uni.reLaunch({
                url: "/pages/layout/index",
              });
            } else {
              uni.showToast({
                icon: "error",
                title: res.msg,
                duration: 2000,
              });
              // 保存失败
              this.status_submit = false;
              // 显示错误信息
            }
          });
        } else {
          //! 校验失败
          this.status_submit = false;
          const errTip = "必填项未填写完成!";
          // 显示错误信息
          this.$showToast({
            show: true,
            text: errTip,
          });
        }
      }

    }
  },
  computed: {
    // 底部导航栏高度 单位px
    navigationBarHeight() {
      return (
        uni.getStorageSync("menuInfo").navigationBarHeight ||
        uni.getStorageSync("menuInfo").statusBarHeight / 2
      );
    },
    // 底部菜单padding掉导航栏的位置
    paddingBottom() {
      if (this.navigationBarHeight) {
        return "padding-bottom:" + this.navigationBarHeight + "px;";
      } else {
        return "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../stepStyles.scss";
</style>